package com.cc.secondKill.commnons;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;

public class ShardedJedisPoolUtils {

    private static Logger logger = LoggerFactory.getLogger(ShardedJedisPoolUtils.class);

    @Deprecated
    public static String get(String key){
        ShardedJedis jedis = null;
        String result = null;
        try {
            jedis = ShardedJedisPools.getResource();
            result = jedis.get(key);
        }catch (Exception e){
            logger.error("Redis get 异常", e);
            ShardedJedisPools.returnBrokenResource(jedis);
            return null;
        }
        ShardedJedisPools.returnResource(jedis);
        return result;
    }

    @Deprecated
    public static String set(String key, String value){
        ShardedJedis jedis = null;
        String result = null;
        try {
            jedis = ShardedJedisPools.getResource();
            result = jedis.set(key, value);
        }catch (Exception e){
            logger.error("Redis set 异常", e);
            ShardedJedisPools.returnBrokenResource(jedis);
            return null;
        }
        ShardedJedisPools.returnResource(jedis);
        return result;
    }

    @Deprecated
    public static String setEx(String key, String value, int ex){
        ShardedJedis jedis = null;
        String result = null;
        try{
            jedis = ShardedJedisPools.getResource();
            result = jedis.setex(key, ex, value);
        }catch (Exception e){
            logger.error("Redis setEx 异常", e);
            ShardedJedisPools.returnBrokenResource(jedis);
            return null;
        }
        ShardedJedisPools.returnResource(jedis);
        return result;
    }

    @Deprecated
    public static Long del(String key){
        ShardedJedis jedis = null;
        Long result = null;
        try {
            jedis = ShardedJedisPools.getResource();
            result = jedis.del(key);
        }catch (Exception e){
            logger.error("Redis del 异常", e);
            ShardedJedisPools.returnBrokenResource(jedis);
            return null;
    }
        ShardedJedisPools.returnResource(jedis);
        return result;
    }

    @Deprecated
    public static Long decr(String key){
        ShardedJedis jedis = null;
        Long result;
        try {
            jedis = ShardedJedisPools.getResource();
            result = jedis.decr(key);
        }catch (Exception e){
            ShardedJedisPools.returnBrokenResource(jedis);
            return null;
        }
        ShardedJedisPools.returnResource(jedis);
        return result;
    }
}
